<template>
  <div class="basic-layout">
    <!-- 侧边栏 -->
    <div :class="{ 'nav-side': true, isCollapse: isCollapse }">
      <div class="log">
        <img src="../assets/logo.png" alt="" />
        <span v-if="!isCollapse" style="transition: none">白霜时尚百货</span>
      </div>
      <el-menu
        :collapse-transition="isCollapse"
        :collapse="isCollapse"
        background-color="#001529"
        text-color="#fff"
        default-active="2"
        class="nav-menu"
        router="true"
      >
        <Tree-Menu :userMenu="userMenu"></Tree-Menu>
      </el-menu>
      <!-- 导航菜单 -->
    </div>
    <!-- 右盒子 -->
    <div :class="{ 'content-right': true, collapse: isCollapse }">
      <!--  右盒子头部 -->
      <div class="nav-top">
        <div class="nav-left">
          <div class="menu-fold" @click="toggle">
            <i class="el-icon-s-fold"></i>
          </div>
          <div class="bread" style="margin-left: 10px">
            <BradCrumb></BradCrumb>
          </div>
        </div>
        <div class="nav-right">
          <el-badge :is-dot="noticeCount" class="notice" type="danger">
            <i class="el-icon-bell"></i>
          </el-badge>
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              {{ userInfo.userName
              }}<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="a"
                  >邮箱:{{ userInfo.userEmail }}</el-dropdown-item
                >
                <el-dropdown-item command="b">退出</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
      <!-- 路由 -->
      <div class="wrapper">
        <router-view class="main-home"></router-view>
      </div>
    </div>
  </div>
</template>
<script>
import TreeMenu from "../components/TreeMenu.vue";
import BradCrumb from "../components/BradCrumb.vue";
export default {
  name: "home",
  data() {
    return {
      isCollapse: false,
      userInfo: this.$store.state.userInfo,
      noticeCount: 0,
      userMenu: [],
    };
  },
  components: {
    TreeMenu,
    BradCrumb,
  },
  mounted() {
    this.getNoticeCount();
    this.getMenuList();
    this.bests();
  },
  methods: {
    handleCommand(command) {
      this.$message("click on item " + command);
      if (command == "b") {
        this.$store.commit("savaUserInfo", "");
        this.userInfo = null;
        this.$router.push("/login");
      }
    },
    toggle() {
      this.isCollapse = !this.isCollapse;
    },
    getNoticeCount() {
      this.$api.noticeCount().then((e) => {
        if (e == 0) {
          this.noticeCount = false;
        } else {
          this.noticeCount = true;
        }
      });
    },
    async getMenuList() {
      const data = await this.$api.getMenuList();
      this.userMenu = data;
    },
    bests() {
      this.$api.best().then((res) => {});
    },
  },
};
</script>
<style lang="scss" scoped>
.basic-layout {
  position: relative;
  .nav-side {
    position: fixed;
    width: 200px;
    height: 100vh;
    background: #001529;
    color: #fff;
    //overflow-y: auto;
    transition: width 0.1s;
    .log {
      width: 100%;
      height: 70px;
      padding: 20px;
      display: flex;
      align-items: center;

      img {
        width: 40px;
        height: 40px;
        margin-right: 10px;
      }
      span {
        font-weight: bold;
      }
    }
    .nav-menu {
      height: calc(100vh - 70px);
      border-right: none;
    }
  }
  .isCollapse {
    width: 70px;
  }

  .content-right {
    transition: width 0.5s;
    margin-left: 200px;

    .nav-top {
      height: 50px;
      line-height: 50px;
      padding: 0 20px;
      display: flex;
      justify-content: space-between;
      border-bottom: 1px solid #ddd;
      .nav-left {
        display: flex;
        align-items: center;
      }
      .nav-right {
        width: 100px;
        display: flex;

        justify-content: space-between;
        align-items: center;
        .notice {
          line-height: 30px;
        }
      }
    }

    .wrapper {
      background: #eef0f3;
      padding: 20px;
      height: calc(100vh - 50px);
      .main-home {
        width: 100%;
        height: 100%;
        padding: 20px;
        background: #fff;
        border-radius: 10px;
      }
    }
  }
  .collapse {
    margin-left: 70px;
  }
}
</style>
